package com.ruoyi.mall.cms.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.cms.domain.CmsSubject;
import com.ruoyi.mall.cms.service.ICmsSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专题Controller
 *
 * @author xuxing
 * @date 2021-02-05
 */
@RestController
@RequestMapping("/cms/subject")
public class CmsSubjectController extends BaseController
{
    @Autowired
    private ICmsSubjectService cmsSubjectService;

    /**
     * 查询专题列表
     */
    @PreAuthorize("@ss.hasPermi('cms:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsSubject cmsSubject)
    {
        startPage();
        List<CmsSubject> list = cmsSubjectService.selectCmsSubjectList(cmsSubject);
        return getDataTable(list);
    }

    /**
     * 导出专题列表
     */
    @PreAuthorize("@ss.hasPermi('cms:subject:export')")
    @Log(title = "专题", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CmsSubject cmsSubject)
    {
        List<CmsSubject> list = cmsSubjectService.selectCmsSubjectList(cmsSubject);
        ExcelUtil<CmsSubject> util = new ExcelUtil<CmsSubject>(CmsSubject.class);
        return util.exportExcel(list, "subject");
    }

    /**
     * 获取专题详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:subject:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cmsSubjectService.selectCmsSubjectById(id));
    }

    /**
     * 新增专题
     */
    @PreAuthorize("@ss.hasPermi('cms:subject:add')")
    @Log(title = "专题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsSubject cmsSubject)
    {
        return toAjax(cmsSubjectService.insertCmsSubject(cmsSubject));
    }

    /**
     * 修改专题
     */
    @PreAuthorize("@ss.hasPermi('cms:subject:edit')")
    @Log(title = "专题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsSubject cmsSubject)
    {
        return toAjax(cmsSubjectService.updateCmsSubject(cmsSubject));
    }

    /**
     * 删除专题
     */
    @PreAuthorize("@ss.hasPermi('cms:subject:remove')")
    @Log(title = "专题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cmsSubjectService.deleteCmsSubjectByIds(ids));
    }
}
